package net.zhengxinyun.performance.api.controller;

import com.alibaba.fastjson.JSONObject;
import net.zhengxinyun.performance.Util.StringUtils;
import net.zhengxinyun.performance.api.service.SysFileService;
import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.filter.IncentiveFilter;
import net.zhengxinyun.performance.filter.IncentiveTypeFilter;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.persistence.Id;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 文件后台处理入口
 */
@RestController
@RequestMapping("/upload")
public class UploadController {

	@Autowired
	private SysFileService sysFileService;

	@GetMapping(value = "/file")
	public String file() {
		return "file";
	}

	@PostMapping(value = "/fileUpload")
	public Result<HashMap<String,Object>> fileUpload(@RequestParam(value = "file") MultipartFile file) {
		HashMap<String,Object> map = sysFileService.save(file);
		if (map==null || map.size() == 0){
			return ResultUtils.getResults(Result.Status.ERROR,"文件上传失败，请重试",null);
		}
		return ResultUtils.getSuccessResults(map);
	}

	@PostMapping("/delete")
	public Result<String> delete(@RequestBody List<String> list){
		String str = sysFileService.delete(list);
		if (!"删除成功".equals(str)){
			return ResultUtils.getResults(Result.Status.ERROR,str);
		}
		return ResultUtils.getSuccessResults();
	}

	@PostMapping("/deletesingle")
	public Result<String> deleteSingle(@RequestBody IncentiveFilter filter)
	{

		String fileId = filter.getFileId();
		if(StringUtils.isBlank(fileId))
		{
			return ResultUtils.getResults(Result.Status.ERROR,"删除失败，filleId不能为空",null);

		}

		String s = sysFileService.deleteSingle(fileId);
		if(!"ok".equals(s))
		{
			return ResultUtils.getResults(Result.Status.ERROR,"删除失败",null);

		}

		return ResultUtils.getSuccessResults("删除成功");
	}


	@PostMapping("/download")
	public Result<String> downloadFile(HttpServletRequest request, HttpServletResponse response,@RequestBody JSONObject jsonObject) {
		String id = jsonObject.get("id").toString();
		String str = sysFileService.downloadFile(request,response,id);
		if (!"下载成功".equals(str)){
			return ResultUtils.getResults(Result.Status.ERROR,"下载失败，请重试");
		}
		return ResultUtils.getSuccessResults();
	}


	@GetMapping("/downloadFile")
	public Result<String> downloadFile(HttpServletRequest request, HttpServletResponse response, String id ) {
		if(StringUtils.isBlank(id))
		{
			return ResultUtils.getResults(Result.Status.ERROR,"无上传文件，请重试");
		}

		String str = sysFileService.downloadFile(request,response,id);
		if (!"下载成功".equals(str)){
			return ResultUtils.getResults(Result.Status.ERROR,"下载失败，请重试");
		}
		return ResultUtils.getSuccessResults();
	}

}
